import Vue from 'vue';

const KEY = "keywords";
const CITY = 'historyCities';

var cache = {
  add: function (keyword) {
    if (!keyword) {
      return;
    }
    let list = [];
    if (localStorage.getItem(KEY)) {
      list = JSON.parse(localStorage.getItem(KEY));
    }
    list.push(keyword)
    localStorage.setItem(KEY, JSON.stringify(list))
  },

  get() {
    return JSON.parse(localStorage.getItem(KEY))
  },

  del(key) {
    let list = JSON.parse(localStorage.getItem(KEY));
    list.splice(list.indexOf(key), 1);
    localStorage.setItem(KEY, JSON.stringify(list))
  }
  ,
  addCity(keyword) {
    if (!keyword) {
      return;
    }
    let list = [];
    if (localStorage.getItem(CITY)) {
      list = JSON.parse(localStorage.getItem(CITY));
    }
   
    // let rs = false;
    // list.forEach((item, index) => {
    //   if (item.name === keyword.name) {
    //     rs = true;
    //   }
    // })
    // let rs = list.every((item)=>item.name !==keyword.name)
    let rs = list.filter((item)=>item.name === keyword.name )
    if(!rs.length){
      list.push(keyword)
    }

    localStorage.setItem(CITY, JSON.stringify(list))
  }
}


Vue.use({
  install(vue) {
    vue.prototype.$cache = cache;
  }
})